package com.bilin.coin.tools;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bilin.coin.R;
import com.bilin.coin.adapter.MyPagerAdapter;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.common.base.BaseFragment;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;


public abstract class AbsViewPagerCustomTabFragment extends BaseFragment{

    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tl_2)
    protected TabLayout tabLayout;

    protected ArrayList<BaseFragment> mFragmentList = new ArrayList<>();
    protected MyPagerAdapter pagerAdapter;


    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        fillFragment(mFragmentList);
        String[] mTitles = fillTabTitle();
        pagerAdapter = new MyPagerAdapter(getChildFragmentManager(), mFragmentList, mTitles);
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        //解决滑动冲突
        initEvent();
    }

    public int currentPos;
    private void initEvent() {
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.setScrollPosition(position, 0f, true);
                currentPos = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    protected abstract String[] fillTabTitle();

    protected abstract void fillFragment(ArrayList<BaseFragment> mFragmentList);

    @Override
    protected void initData() {

    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_common_viewpager_tab_cus;
    }

}
